package com.example.a

import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.provider.Settings
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import com.example.user.LoginActivity
import com.google.android.material.navigation.NavigationView

class MainViewActivity : AppCompatActivity(), View.OnClickListener {

    private lateinit var toolBar: androidx.appcompat.widget.Toolbar
    private lateinit var navView: NavigationView
    private lateinit var drawerLayout: DrawerLayout
    private lateinit var menuButton: ImageView
    private lateinit var loginButton: TextView
    private lateinit var wifiButton: TextView
    private lateinit var userPortraitImageView: ImageView
    var isLogin: Boolean = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        // 读取SharedPreferences中的登录状态

        // 未登录时显示当前页面
        setContentView(R.layout.fiberhome_home_device_activity)

        //初始化控件
        toolBar = findViewById(com.example.a.R.id.title_bar)
        navView = findViewById(com.example.a.R.id.navi_view)
        drawerLayout = findViewById(com.example.a.R.id.draw_layout)
        menuButton = findViewById(com.example.a.R.id.title_bar_left_view)
        loginButton = findViewById(com.example.a.R.id.login_view)
        wifiButton = findViewById(com.example.a.R.id.goto_connect_wifi_view)

        // 获取侧边栏头部视图和头像
        if (::navView.isInitialized) {
            val headerView = navView.getHeaderView(0)
            if (headerView != null) {
                userPortraitImageView = headerView.findViewById(com.example.a.R.id.iv_user_portrait)
                // 绑定头像点击监听器
                if (::userPortraitImageView.isInitialized) {
                    userPortraitImageView.setOnClickListener(this)
                }
            }
        }

        //绑定监听器
        menuButton.setOnClickListener(this)
        loginButton.setOnClickListener(this)
        wifiButton.setOnClickListener(this)

    }

    override fun onStart() {
        val preferences: SharedPreferences = getSharedPreferences("user_info", MODE_PRIVATE)
        isLogin = preferences.getBoolean("is_login", false)

        super.onStart()
        if (isLogin) {
            val intent = Intent(this, MainViewActivityLogined::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
            startActivity(intent)
            finish()
        }
    }

    override fun onClick(v: View) {
        //活动页面跳转
        when (v.id) {
            com.example.a.R.id.title_bar_left_view -> {
                if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
                    drawerLayout.closeDrawer(GravityCompat.START)
                } else {
                    drawerLayout.openDrawer(GravityCompat.START)
                }
            }
            com.example.a.R.id.login_view -> {
                startActivity(Intent(this, LoginActivity::class.java))
            }
            com.example.a.R.id.goto_connect_wifi_view -> {
                val intent = Intent(Settings.ACTION_WIFI_SETTINGS)
                // 可选：防止 ActivityNotFoundException
                if (intent.resolveActivity(packageManager) != null) {
                    startActivity(intent)
                }
            }
            com.example.a.R.id.iv_user_portrait -> {
                // 点击侧边栏头像跳转到登录界面
                startActivity(Intent(this, LoginActivity::class.java))
            }
        }
    }
}